package com.outsourcing.bottle.ui;

import java.util.HashMap;

import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.TextUtil;
import com.outsourcing.bottle.util.UICore;
/**
 * 
 * @author 06peng
 *
 */
public class ForgetPasswordActivity extends BasicActivity implements BasicUIEvent, OnClickListener {

	private final int reset = 1;
	private final int reset_success = 2;
	private final int reset_error = 3;
	
	private ImageView close;
	private EditText emailView;
	private Button resetView;
	private String email;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.forget_password);
		initWidget();
	}
	
	private void initWidget() {
		close = (ImageView) findViewById(R.id.close);
		close.setOnClickListener(this);
		emailView = (EditText) findViewById(R.id.forget_password_email);
		emailView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
					checkReset();
				}
				return false;
			}
		});
		resetView = (Button) findViewById(R.id.forget_password_reset);
		resetView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == close) {
			finish();
		} else if (v == resetView) {
			checkReset();
		}
	}
	
	private void checkReset() {
		email = emailView.getText().toString().trim();
		if (email.equals("")) {
			onToast(getString(R.string.login_txt_reset_error));
			return;
		} else if (!TextUtil.isValidEmail(email)) {
			onToast(getString(R.string.login_txt_register_error_email));
			return;
		}
		UICore.eventTask(this, this, reset, getString(R.string.init_data), null);
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case reset:
			reset();
			break;

		default:
			break;
		}
	}
	
	private void reset() {
		String url = UrlConfig.reset_password;
		HashMap<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("email", email);
		try {
			String result = HttpUtils.doPost(this, url, paramsMap);
			if (result != null) {
				JSONObject obj = new JSONObject(result);
				JSONObject resultObj = obj.getJSONObject("results");
				int sucess = resultObj.getInt("success");
				if (sucess == 1) {
					String successmsg = resultObj.getString("successmsg");
					Message msg = Message.obtain(handler, reset_success, successmsg);
					handler.sendMessage(msg);
				} else {
					String errmsg = resultObj.getString("errmsg");
					Message msg = Message.obtain(handler, reset_error, errmsg);
					handler.sendMessage(msg);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case reset_success:
				onToast((String) msg.obj);
				break;
			case reset_error:
				onToast((String) msg.obj);
				break;
			default:
				break;
			}
		}
	};
}
